package vtc;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import com.ib.client.Contract;

public class IBFeedUnit extends InputUnit
{
	IBBroker broker_;
	Contract contract_; 
	String genericTickList_; 
	boolean snapShot_;
		
	IBFeedUnit(String id, VtcArrayBlockingQueue outQueues[], IBBroker broker, Contract contract, String genericTickList, boolean snapShot)
	{
		super(id, outQueues);
		broker_ = broker;
		contract_ = contract; 
		genericTickList_ = genericTickList; 
		snapShot_ = snapShot;
	}
	
	public VtcTimeTaggedEvent inputProcess() throws InterruptedException
	{
		int tickerId = broker_.getNextValidId();
		System.out.print("tickerId: " + tickerId + "\n");
		broker_.reqMktData(tickerId, contract_, genericTickList_, snapShot_);
		try
		{
			TimeUnit.MILLISECONDS.sleep(500);
		}
		catch(InterruptedException e)
		{
			System.out.print("Interrupted during sleep in IBFeedUnit::inputProcess()");
			throw e;
		}
		DataEventIB data = broker_.getMktData(tickerId);
		
		if(data!=null)
			System.out.print("Data event from Market data:" + data.toString() + "\n");
		Date date = new Date();
		
		
		VtcTimeTaggedEvent event = new VtcTimeTaggedEvent(data, date);
		
		return event;
	}
}